/*
 * Copyright (C) 2009 Ionut Dediu <deionut@yahoo.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// BtDiscoverer.h

#ifndef __BT_DISCOVERER_H__
#define __BT_DISCOVERER_H__

#include <e32base.h>
#include <e32std.h>
#include <btsdp.h>
#include <btextnotifiers.h>

class MBtAttributeObserver
	{
	public:
		virtual void RemotePortFound(TInt aPort) = 0;
	};

class MBtDiscoveryObserver
{
public:

	virtual void RemoteDeviceSelected(TInt aErrorCode) = 0;
	virtual void RemoteServiceFound(TInt aErrorCode) = 0;

};

class CBtDiscoverer : public CActive,
						 public MSdpAgentNotifier,
						 public MBtAttributeObserver
{
public:

/*!
  @function NewL

  @discussion Create a CBtDiscoverer object
  @result a pointer to the created instance of CBtCamDiscoverer
  */
    static CBtDiscoverer* NewL(MBtDiscoveryObserver& aDiscoveryObserver);

/*!
  @function NewLC

  @discussion Create a CBtDiscoverer object
  @result a pointer to the created instance of CBtDiscoverer
  */
    static CBtDiscoverer* NewLC(MBtDiscoveryObserver& aDiscoveryObserver);

/*!
  @function ~CBtDiscoverer

  @discussion Destroy the object and release all memory objects
  */
    ~CBtDiscoverer();

	void SelectRemoteDevice();
	void FindRemoteService(TBTDevAddr btDevAddr);
	TBTDevAddr BTDevAddr();
	const TDesC& BTDevName();
	TBool IsValidBDAddr();

protected: // from CActive

/*!
  @function DoCancel

  @discussion Cancel any outstanding requests
  */
    void DoCancel();

/*!
  @function RunL

  @discussion Respond to an event
  */
    void RunL();

private:

/*!
  @function CBtDiscoverer

  @discussion Constructs this object
  */
    CBtDiscoverer(MBtDiscoveryObserver& aDiscoveryObserver);

/*!
  @function ConstructL

  @discussion Performs second phase construction of this object
  */
    void ConstructL();

	const TUUID& ServiceClass();

private:

    // Member variables
	MBtDiscoveryObserver& iDiscoveryObserver;
	RNotifier iDeviceSelector;
	TBool iIsDeviceSelectorConnected;
	TBTDeviceSelectionParamsPckg iDeviceSelectionFilter;
	TBTDeviceResponseParamsPckg iDeviceSelectionResponse;
	TUUID iServiceClass;
    CSdpAgent* iAgent;
	CSdpSearchPattern* iSdpSearchPattern;
	CSdpAttrIdMatchList* iAttrMatchList;
	TInt iPort;
	TBool iContinueServiceDiscovery;

	enum TDiscovererState
		{
		EGettingDevice,
		EGettingService
		} iDiscoveryState;

public:

// from MSdpAgentNotifier:

	void AttributeRequestComplete(TSdpServRecordHandle aHandle, TInt aError);

	void AttributeRequestResult(
		TSdpServRecordHandle aHandle,
		TSdpAttributeID aAttrID,
		CSdpAttrValue* aAttrValue);

	void NextRecordRequestComplete(
		TInt aError,
		TSdpServRecordHandle aHandle,
		TInt aTotalRecordsCount);

	TInt Port();

// from MBtAttributeObserver:
	void RemotePortFound(TInt aPort);

};

class TBtAttributeParser : public MSdpAttributeValueVisitor
{
public:

/*!
  @function TBtAttributeParser

  @discussion Constructs this object
  */
    TBtAttributeParser(MBtAttributeObserver& aAttributeObserver);

// from MSdpAttributeValueVisitor:

	void EndListL();
	void StartListL(CSdpAttrValueList &aList);
	void VisitAttributeValueL(CSdpAttrValue &aValue, TSdpElementType aType);

private:
    // Member variables
	MBtAttributeObserver& iAttributeObserver;
	TBool iRfcommPortExpected;
};

#endif //__BT_DISCOVERER_H__

